package personal.zq.practice.common.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 根据资源名称和资源Id生成查询文件位置语句
 */
public class GetSqlByResourceId {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<Map<String, String>> ins = new ArrayList<>();
        while (in.hasNextLine()) {
            String name = in.next();
            if ("0".equals(name)) {
                break;
            }
            Long resourceId = Long.parseLong(in.next());
            long timestamp = resourceId >> 24;
            timestamp += 1510156800000L;
            LocalDate date = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
            int day = date.getDayOfMonth();
            int bank = day / 5;
            int m = date.getMonthValue();
            String mo = m + "";
            if (m < 10) {
                mo = "0" + mo;
            }
            int y = date.getYear();
            String banks = "_" + y + mo + "_" + bank;
            Map map = new HashMap<>();
            map.put("name", name);
            map.put("banks", banks);
            map.put("resourceId", String.valueOf(resourceId));
            ins.add(map);
        }
        ins.forEach(m -> {
            System.out.println("SELECT '" + m.get("name") + "' as 名称 , a.file_name 文件名 ,b.file_storage_data_node " + "节点,b" + ".file_storage_path " +
                    "路径" + " from blackhole" + ".zlsddfs_file_info" + m.get("banks") + " a, " + "blackhole" + ".zlsddfs_file_storage" + m.get(
                            "banks") + " b where a.resource_id=b.resource_id and a.resource_id=" + m.get("resourceId") + " " + "union ");

        });
    }
}